#!/sbin/bash

exec 2>/dev/console
exec 1>/dev/console

echo -e ""
echo -e "               *************************************************************************************"
echo -e "               *                                                                                   *" 
echo -e "               * Channl.TV Patchstick                                                              *"
echo -e "               * Install the Channl.TV application                                                 *"
echo -e "               *                                                                                   *" 
echo -e "               *                                                                                   *" 
echo -e "               * This installation will make modifications to the software stack on your device.   *"
echo -e "               *                                                                                   *" 
echo -e "               * This installer and the Channl.TV software are made for demonstration purpose only *" 
echo -e "               * and provided on an 'as is' basis.                                                 *" 
echo -e "               *                                                                                   *" 
echo -e "               * The author(s) assumes no responsibility or liability for any damage, direct or    *" 
echo -e "               * indirect claims as a result of using this installer and or related application(s) *"
echo -e "               * and or related services.                                                          *"
echo -e "               * To abort the installation : unpower the device and remove the patchstick.         *"
echo -e "               *                                                                                   *" 
echo -e "               * INSTALL AT YOUR OWN RISK OR ABORT THE INSTALLATION NOW !                          *"
echo -e "               *                                                                                   *" 
echo -e "               *************************************************************************************"
echo
echo -e "            Installation starts within"
echo -e "            30 seconds"
echo -e ""
echo -e ""
/sbin/sleep 10
echo -e "            20 seconds"
echo -e ""
echo -e ""
/sbin/sleep 10
echo -e "            10 seconds"
echo -e ""
echo -e ""
echo -e ""
/sbin/sleep 10
echo
echo
echo -e "            Installation Started, Please wait..."
echo
echo

exec 2>/dev/null
exec 1>/dev/null

# echo "        * remounting / r/w"
# /sbin/mount -uw /   

echo "*** Mounting OSBoot partition r/o"
/sbin/mount -t hfs -r /dev/disk0s3 /OSBoot

echo "*** Re-mounting OSBoot partition r/w"
/sbin/mount -uw /OSBoot

echo "*** Mounting stuff partition r/o"
/sbin/mount -t hfs -r /dev/disk1s2 /stuff

#echo "*** Keeping the OSBoot partition r/w for plugins"
touch /OSBoot/.readwrite

if [ -d /OSBoot/dev/ ]
then

  for i in /stuff/installer.d/*; do
    echo "        --- ${i} ---"
    /sbin/bash "${i}"
  done

  /sbin/sync &>/dev/null
  /sbin/umount /OSBoot
  echo
  echo "*** All Done!"

else
  echo "*** ! OSBoot not mounted, not installing or modifying anything" > /dev/console
  /sbin/sleep 5	
fi

echo "*** Setting internal disk as boot volume"
/sbin/bless --device /dev/disk0s3 --setBoot


exec 2>/dev/console
exec 1>/dev/console

echo
echo
echo "*"
echo "*                  Channl.TV Installation Done."
echo "*"
echo "*                  PLEASE REMOVE THE PATCHSTICK AND REBOOT THE DEVICE...."
echo "*"
echo
echo
echo
/sbin/sleep 3600
	
exit 0